package com.ff.neuropp.controller;

import java.util.Map;

import org.dom4j.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ff.neuropp.domain.IosUser;
import com.ff.neuropp.service.IosUserService;
import com.ff.neuropp.util.ResultTool;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * app端 用户登录
 */
@RestController
@RequestMapping("/app/user")
@Api(value = "IosController", tags = {"app用户接口"})
public class IosUserController {
	
    @Autowired
    private IosUserService iosUserService;


    @PostMapping("/sendCode")
    @ApiOperation(value = "谷歌发送验证码", notes = "谷歌发送验证码")
    public  Map<String,Object> sendCode(@ApiParam(required = true,value = "谷歌邮箱地址") @RequestBody IosUser IosUser){
        return iosUserService.sendCode(IosUser.getEmail());

    }


    @PostMapping("/register")
    @ApiOperation(value = "用户注册",notes = "用户注册")
    public Map<String,Object> addUser(@RequestBody IosUser IosUser){
        Map<String,Object> map = iosUserService.addUser(IosUser);
        return ResultTool.genSuccessResult(map);
    }


    @PostMapping("/login")
    @ApiOperation(value = "用户登录",notes = "用户登录")
    public Map<String,Object> login(@RequestBody IosUser IosUser){
        Map<String,Object> map = iosUserService.login(IosUser);
        return ResultTool.genSuccessResult(map);
    }
    
  @PostMapping("/update")
  @ApiOperation(value = "更新用戶信息", notes = "更新用戶信息")
  public Map<String, Object> updateUser(@RequestBody IosUser iosUser) {
     IosUser bak = iosUserService.findByUserId(iosUser.getUserId());
      if (null == bak) {
          return ResultTool.genFailResult(ResultTool.ErrorCode_1, "编辑错误");
      }
      if(org.apache.commons.lang.StringUtils.isNotEmpty(iosUser.getHeadUrl()) && null!=iosUser.getHeadUrl()) {
    	  bak.setHeadUrl(iosUser.getHeadUrl()); 
      }
      if(org.apache.commons.lang.StringUtils.isNotEmpty(iosUser.getSign()) && null!=iosUser.getSign()) {
    	  bak.setSign(iosUser.getSign());
      }
      if(org.apache.commons.lang.StringUtils.isNotEmpty(iosUser.getContactInfo()) && null!=iosUser.getContactInfo()) {
    	  bak.setContactInfo(iosUser.getContactInfo());
      }
      iosUserService.saveUser(bak);
      return ResultTool.genSuccessResult("修改成功");
  }

}
